//
//  XVCMapTypeShow.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/25.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCMapTypeShow.h"
#import "XVVSelectedExhibitionHall.h"
#import "XXTool.h"
#import "XVCDetailMapHall.h"
#import "RXMLExhibitorInformationModel.h"

@implementation XMLCollectData

@end

@implementation XMLMapType

@end

@interface XVCMapTypeShow ()<XVVSelectedExhibitionHallDelegate>

@property (nonatomic) XVVSelectedExhibitionHall *sExH;

@end

@implementation XVCMapTypeShow
{
    NSMutableArray <XMLMapType*> *mapTypeArray;
    
    NSMutableArray *exhibitor_id_array;
}

-(XVVSelectedExhibitionHall*)sExH
{
    if (_sExH==nil)
    {
        _sExH = [[XVVSelectedExhibitionHall alloc] init];
        _sExH.delegate = self;
        [self.view addSubview:_sExH];
    }
    return _sExH;
}

-(void)initParameter
{
    
}

-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"展馆地图", nil)];
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"更多"] style:UIBarButtonItemStylePlain target:self action:@selector(clickleft)]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getMap) name:@"ClickMapShow" object:nil];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)initData
{
    
    
}

-(void)refreshLayout:(CGRect)frame
{
    self.sExH.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getMap];
}

-(void)getMap
{
//    NSString *id=[UserInfo shareUserInfo].userID;
//    if( ![id isEqualToString:@""]){
//        [DKHTTPRequest sendDataWithShortUrl:@"favorites_visitor"
//                                      Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"userid=%@&passwd=%@",[UserInfo shareUserInfo].userID,[UserInfo shareUserInfo].userPassword]]
//                                 ImageArray:nil
//                                    TimeOut:10
//                                 Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
//                                     if (serverDic==nil||[[serverDic objectForKey:@"code"] intValue]==100)
//                                     {
//                                         mapTypeArray = [NSMutableArray array];
//                                         XMLMapType *temp = [[XMLMapType alloc] init];
//                                         temp.mtype = @"1";
//                                         temp.maplistname = NSLocalizedString(@"平面图", nil);
//                                         [mapTypeArray addObject:temp];
//                                     }
//                                     else if ([[serverDic objectForKey:@"code"] intValue]==200)
//                                     {
//                                         mapTypeArray = [NSMutableArray array];
//                                         XMLMapType *temp = [[XMLMapType alloc] init];
//                                         temp.mtype = @"1";
//                                         temp.maplistname = NSLocalizedString(@"平面图", nil);
//                                         [mapTypeArray addObject:temp];
//                                         
//                                         /*
//                                          if([[serverDic objectForKey:@"total"] intValue]>0)
//                                          {
//                                          XMLMapType *collectTemp = [[XMLMapType alloc] init];
//                                          collectTemp.mtype = @"2";
//                                          collectTemp.maplistname = NSLocalizedString(@"我收藏的展商", nil);
//                                          [mapTypeArray addObject:collectTemp];
//                                          
//                                          exhibitor_id_array = [NSMutableArray array];
//                                          NSArray *list = [serverDic objectForKey:@"exlist"];
//                                          for (NSDictionary *temp in list)
//                                          {
//                                          [exhibitor_id_array addObject:[temp objectForKey:@"exhibitor_id"]];
//                                          }
//                                          }
//                                          */
//                                     }
//                                     [self.sExH setViewData:mapTypeArray];
//                                     return YES;
//                                 } IsBuffering:YES
//                          IsNeedReplaceWrap:NO
//                            IsUseUpLoadHead:NO];
//
//    }
//    else
    {
        mapTypeArray = [NSMutableArray array];
        XMLMapType *temp = [[XMLMapType alloc] init];
        temp.mtype = @"1";
        temp.maplistname = NSLocalizedString(@"平面图", nil);
        [mapTypeArray addObject:temp];
        [self.sExH setViewData:mapTypeArray];
    }
}

#pragma mark XVVSelectedExhibitionHallDelegate
-(void)selectedExhibitionhallDidClickHallWithHallMapType:(NSInteger)type
{
    if(type == 1)
    {
        //平面图mtype为2
        [XVCDetailMapHall pushDetailMapHallVCWithSourceVC:self
                                                 HallType:DetailMapHallMap
                                                    mtype:2
                                              collectData:nil];
    }
    else if(type==2)
    {
        NSString *realmSearchString = @"";
        for (NSString *temp in exhibitor_id_array)
        {
            realmSearchString = [NSString stringWithFormat:@"%@OR ExhibitorID = '%@'",realmSearchString,temp];
        }
        realmSearchString = [realmSearchString substringFromIndex:2];
        
        RLMResults *resluts = [RXMLExhibitorInformationModel objectsInRealm:[RLMRealm realmWithPath:[XXTool dataBasePath]] where:realmSearchString];
        
        NSMutableArray *hallNoArray = [NSMutableArray array];
        for (RXMLExhibitorInformationModel *temp in resluts)
        {
            NSString *stand = [temp.StandNo substringToIndex:1];
            if (![hallNoArray containsObject:stand])
            {
                [hallNoArray addObject:stand];
            }
        }

        [hallNoArray sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2)
        {
            if ([obj1 integerValue] > [obj2 integerValue])
            {
                return (NSComparisonResult)NSOrderedDescending;
            }
            if ([obj1 integerValue] < [obj2 integerValue])
            {
                return (NSComparisonResult)NSOrderedAscending;
            }
            return (NSComparisonResult)NSOrderedSame;
        }];
        
        NSMutableArray *standNo_array = [NSMutableArray array];
        for (NSString *temp in hallNoArray)
        {
            XMLCollectData *data = [[XMLCollectData alloc] init];
            data.tag = temp;
            data.hallNo_array = [NSMutableArray array];
            
            for (RXMLExhibitorInformationModel *temp_data in resluts)
            {
                NSString *stand = [temp_data.StandNo substringToIndex:1];
                if ([stand isEqualToString:temp])
                {
                    [data.hallNo_array addObject:temp_data.StandNo];
                }
            }
            [standNo_array addObject:data];
        }
    
        [XVCDetailMapHall pushDetailMapHallVCWithSourceVC:self
                                                 HallType:DetailMapHallCollect
                                                    mtype:0
                                              collectData:standNo_array];
    }
}

-(void)clickleft
{
    //[self.navigationController.sideViewController showLeftView];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
